<template>
  <div class="currentPage">
    <market-header />
    <div class="line" />
    <div class="cateArea">
      <el-carousel :autoplay="true" height="600px">
        <el-carousel-item
          v-for="(item, index) in bannerArr"
          :key="index"
          style="width:100%;"
        >
          <img class="bannerImg" :src="imgPrefix + item.uri" alt="">
        </el-carousel-item>
      </el-carousel>
      <div class="w1300 category">
        <div class="title"><svg-icon icon-class="m-category" /> Categorize</div>
        <div class="cateMenu">
          <div class="stair">
            <ul>
              <li
                v-for="(item, index) of categoryArr"
                :key="index"
                class="item item_first"
              >
                <router-link
                  :to="{
                    path: '/marketing/cate',
                    query: { id: item.parent.id }
                  }"
                >
                  <svg-icon icon-class="m-shopping" /> {{ item.parent.name }}
                </router-link>
                <div v-if="item.childs.length > 0" class="cateChildren">
                  <div class="ulList">
                    <div v-for="(it, i) in item.childs" :key="i" class="item">
                      <router-link
                        :to="{
                          path: '/marketing/cate',
                          query: { id: it.parent.id }
                        }"
                      >
                        {{ it.parent.name }}
                      </router-link>
                      <div class="cateThird">
                        <router-link
                          v-for="(t, n) in it.childs"
                          :key="n"
                          :to="{
                            path: '/marketing/cate',
                            query: { id: t.parent.id }
                          }"
                        >{{ t.parent.name }}</router-link>
                      </div>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="products">
      <!-- POD专区 -->
      <div v-if="podData.length > 0" class="floor w1300">
        <div class="floorTitle">
          <span>
            <svg-icon icon-class="m-floor" />
            POD
          </span>
          <router-link
            :to="{
              path: '/marketing/cate',
              query: { id: 1 }
            }"
          >
            View more
          </router-link>
        </div>
        <div class="floorBox">
          <div v-for="(item, index) in podData" :key="index" class="item">
            <div class="imgBox">
              <el-image
                :src="imgPrefix + item.image"
                fit="cover"
                @click="goDetail(item.product.id)"
              />
            </div>
            <div class="info">
              <p :title="item.product.name" @click="goDetail(item.product.id)">
                {{ item.product.name }}
              </p>
              <div class="itemPrice">$ {{ item.product.salePrice / 100 }}</div>
              <div class="itemBtm">
                <el-dropdown
                  @command="(command) => gather(item.product.id, command)"
                >
                  <el-button type="danger" size="mini" round>Gather</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="1">SHOPIFY</el-dropdown-item>
                    <el-dropdown-item
                      :command="2"
                    >WOOCOMMERCE</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div class="collect">
              <svg-icon
                style="color: #fff;"
                icon-class="m-collect"
                @click="collectFavorite(item)"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 选择优品 -->
      <div v-if="dropshippingData.length > 0" class="floor w1300 blue">
        <div class="floorTitle">
          <span>
            <svg-icon icon-class="m-floor" />
            DROPSHIP
          </span>
          <router-link
            :to="{
              path: '/marketing/cate',
              query: { id: 2 }
            }"
          >View more</router-link>
        </div>
        <div class="floorBox">
          <div
            v-for="(item, index) in dropshippingData"
            :key="index"
            class="item"
          >
            <div class="imgBox">
              <el-image
                fit="cover"
                :src="imgPrefix + item.image"
                @click="goDetail(item.product.id)"
              />
            </div>
            <div class="info">
              <p :title="item.product.name" @click="goDetail(item.product.id)">
                {{ item.product.name }}
              </p>
              <div class="itemPrice">$ {{ item.product.salePrice / 100 }}</div>
              <div class="itemBtm">
                <el-dropdown
                  @command="(command) => gather(item.product.id, command)"
                >
                  <el-button size="mini" round>Gather</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="1">SHOPIFY</el-dropdown-item>
                    <el-dropdown-item
                      :command="2"
                    >WOOCOMMERCE</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div class="collect">
              <svg-icon
                style="color: #fff;"
                icon-class="m-collect"
                @click="collectFavorite(item)"
              />
            </div>
          </div>
        </div>
      </div>
    </div>

    <market-footer />
  </div>
</template>

<script>
import { bannerList } from '@/api/common';
import { collectFavorite, gather } from '@/api/product';
import { marketCategory, dropshipping, pod } from '@/api/market';
import MarketHeader from './components/header';
import MarketFooter from './components/footer';
export default {
  components: {
    MarketHeader,
    MarketFooter
  },
  data() {
    return {
      bannerArr: [],
      categoryArr: [],
      podData: [],
      dropshippingData: []
    };
  },
  mounted() {
    document.getElementsByClassName('currentPage')[0].style.minHeight =
      document.documentElement.clientHeight + 'px';
    this.getBanner();
    this.marketCategory();
    this.homeData();
  },
  methods: {
    getBanner() {
      bannerList().then((res) => {
        this.bannerArr = res.data;
      });
    },
    marketCategory() {
      marketCategory().then((res) => {
        this.categoryArr = res.data;
      });
    },
    homeData() {
      dropshipping({
        pageNo: 1,
        pageSize: 10
      }).then((res) => {
        res.data.data.map((item) => {
          if (item.images.length > 0) {
            item.image = item.images[0].uri;
          }
        });
        this.dropshippingData = res.data.data;
      });
      pod({
        pageNo: 1,
        pageSize: 8
      }).then((res) => {
        res.data.data.map((item) => {
          if (item.images.length > 0) {
            item.image = item.images[0].uri;
          }
        });
        this.podData = res.data.data;
      });
    },
    goDetail(id) {
      this.$router.push({ path: '/marketing/detail', query: { id: id }});
    },
    collectFavorite(item) {
      collectFavorite([item.product.id]).then((res) => {
        if (res.code === 200) {
          this.$message.success('collect success');
        }
      });
    },
    gather(id, command) {
      gather({
        id: id,
        storeIds: [command]
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success('gather success');
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
ul,
li {
  margin: 0;
  list-style: none;
}
.w1300 {
  width: 1300px;
  margin: 0 auto;
}
img {
  background: #fafafa;
}
.currentPage {
  overflow: hidden;
  position: relative;
  background: #f6f6f6;
}
.line {
  height: 42px;
  background: #fff;
}
.cateArea {
  position: relative;
  .bannerImg {
    display: block;
    width: 100%;
    height: 600px;
    object-fit: cover;
  }
  .category {
    position: absolute;
    top: 0;
    left: 50%;
    margin-left: -600px;
    font-family: 'Segoe UI';
    height: 100%;
    z-index: 99;
    > .title {
      width: 200px;
      height: 42px;
      line-height: 42px;
      background: #e42d03;
      color: #fff;
      text-align: center;
      margin-top: -42px;
      font-size: 20px;
    }
    .cateMenu {
      display: inline-block;
      position: relative;
      height: 100%;
      > div {
        float: left;
      }
      .stair {
        width: 200px;
        height: 100%;
        background: rgba(0, 0, 0, 0.4);
        .item {
          padding: 8px 0;
          color: #fff;
          border-top: 1px solid transparent;
          border-bottom: 1px solid transparent;
          &.item_first {
            text-align: center;
          }
          &.active {
            color: #e42d03;
            background: #fff;
            border-top: 1px solid #ddd;
            border-bottom: 1px solid #ddd;
            svg {
              color: #e42d03;
            }
          }
          &:hover {
            color: #e42d03;
            background: #fff;
            svg {
              color: #e42d03;
            }
            .cateChildren {
              display: block;
            }
          }
          a {
            font-size: 17px;
          }
        }
      }
      .cateChildren {
        position: absolute;
        left: 200px;
        top: 0;
        display: none;
        background: rgba(255, 255, 255, 1);
        &.firstChoosed {
          display: block;
        }
        .ulList {
          display: block;
          height: 600px;
          padding: 0 0 30px;
          .item {
            padding: 10px 10px 10px 30px;
            display: flex;
            width: 200px;
            &:hover {
              background: rgba(232, 232, 232, 1);
              a {
                color: #000;
              }
              .cateThird {
                display: flex;
              }
            }
            > a {
              color: #333;
              font-size: 16px;
            }
            .cateThird {
              position: absolute;
              left: 200px;
              top: -1px;
              display: none;
              flex: 1;
              overflow: hidden;
              background: rgba(232, 232, 232, 1);
              width: 200px;
              height: 601px;
              flex-direction: column;
              a {
                padding: 10px 10px 10px 30px;
                text-align: left;
              }
            }
          }
        }
        > .item {
          width: 33.33%;
          height: 150px;
          .title {
            line-height: 40px;
            font-weight: 600;
          }
          .lastChildren {
            .item {
              padding-bottom: 10px;
              &:hover {
                color: #e42d03;
              }
            }
          }
        }
      }
    }
  }
}
.products {
  padding-top: 20px;
  overflow: hidden;
  .floor {
    margin-bottom: 20px;
    .floorTitle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 30px;
      height: 48px;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.16);
      font-size: 20px;
      i {
        color: #e42d03;
      }
      a {
        font-size: 20px;
        color: #666;
        &:hover {
          text-decoration: underline;
          color: #464646;
        }
      }
    }
    > .title {
      font-size: 18px;
      line-height: 60px;
      font-weight: 600;
    }
    .floorBox {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      .item {
        position: relative;
        float: left;
        width: 300px;
        margin-top: 24px;
        margin-right: 33px;
        &:nth-child(4n + 4) {
          margin-right: 0;
        }
        .imgBox {
          width: 100%;
          height: 174px;
          &::before {
            contain: '';
            width: 100%;
            height: 100%;
            border: 1px dotted #ddd;
          }
          > .el-image {
            width: 100%;
            height: 174px;
            cursor: pointer;
          }
        }
        .info {
          font-size: 18px;
          font-family: 'Segoe UI';
          text-align: center;
          background: #fff;
          p {
            padding: 10px 10px 0;
            line-height: 24px;
            color: rgb(51, 51, 51);
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            &:hover {
              cursor: pointer;
              color: red;
            }
          }
          .itemPrice {
            font-weight: 600;
            line-height: 40px;
          }
          .itemBtm {
            display: flex;
            justify-content: center;
            align-items: center;
            padding-bottom: 15px;
            .el-button {
              padding: 3px 8px;
              background: rgba(228, 45, 3, 1);
              font-size: 14px;
            }
          }
        }
        .collect {
          position: absolute;
          right: 0;
          bottom: 0;
          width: 45px;
          height: 45px;
          &::before {
            position: absolute;
            bottom: 0;
            right: 0;
            content: '';
            width: 0;
            height: 0;
            border-bottom: 45px solid red;
            border-left: 45px solid transparent;
          }
          svg {
            position: absolute;
            bottom: 5px;
            right: 5px;
            cursor: pointer;
          }
        }
        // .collect
      }
    }
    &.blue {
      .floorTitle {
        i {
          color: #3ea7f6;
        }
      }
      .floorBox {
        .item {
          width: 240px;
          margin-right: 25px;
          &:nth-child(4n + 4) {
            margin-right: 25px;
          }
          &:nth-child(5n + 5) {
            margin-right: 0;
          }
          .itemBtm {
            .el-button {
              color: #fff;
              background-color: #3499e5;
              &:hover {
                background-color: #3ea7f6;
              }
            }
          }
          .collect {
            &::before {
              border-bottom-color: #3499e5;
            }
          }
        }
      }
    }
  }
}
</style>
